package com.springboot.controller;

import com.springboot.model.*;
import com.springboot.service.AssistantMessageListService;
import com.springboot.service.AssistantService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 本控制台用于助理取消患者预约
 */
@Controller
public class AssistantCancelAppointment {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    AssistantMessageListService assistantMessageListService;
    @Autowired
    AssistantService assistantService;


    @CrossOrigin
    @RequestMapping(value = "/assistantCancelAppointment")
    public @ResponseBody
    String assistantCancelAppointment(@RequestParam("assistantTel") String assistantTel,
                                      @RequestParam("doctorTel") String doctorTel,
                                      @RequestParam("patientTel") String patientTel,
                                      @RequestParam("bookDate") String bookDate,
                                      @RequestParam("bookTime") String bookTime){
        System.out.println("助理取消预约开始");
        String result="True";
        try{
            UserLoginForm userLoginForm1=userLoginService.findUserByTel(doctorTel);
            Integer doctorId=userLoginForm1.getUserId();
            System.out.println(doctorId);
            UserLoginForm userLoginForm2=userLoginService.findUserByTel(patientTel);
            Integer patientId=userLoginForm2.getUserId();
            UserLoginForm userLoginForm3=userLoginService.findUserByTel(assistantTel);
            Integer assistantId=userLoginForm3.getUserId();

            OrderForm orderForm=new OrderForm();
            orderForm.setDoctorId(doctorId);
            orderForm.setPatientId(patientId);
            orderForm.setBootTime(bookTime);
            //日期转为sql
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date d = null;
            try {
                d = format.parse(bookDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            java.sql.Date date = new java.sql.Date(d.getTime());
            orderForm.setBootDate(date);
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = sdf.format(now);//今日日期
            System.out.println(str);
            try {
                d = sdf.parse(str);
            } catch (Exception e) {
                e.printStackTrace();
                result="False";
            }
            java.sql.Date nowdate = new java.sql.Date(d.getTime());
            MessageInform messageInform=new MessageInform();
            messageInform.setBookDate(date);
            messageInform.setBookTime(bookTime);
            messageInform.setDoctorId(doctorId);
            messageInform.setPatientId(patientId);
            messageInform.setMessageDatetime(nowdate);
            messageInform.setMessageMark(4);
            messageInform.setMessageState1("未读");
            messageInform.setMessageState2("未读");
            int x=assistantMessageListService.insertCancelMessageInform(messageInform);
            int messageId=assistantMessageListService.getMessageId();
            AssistantMessageInform assistantMessageInform=new AssistantMessageInform(assistantId,messageId,"未读",1);
            int b=assistantMessageListService.insertCancelAssistentMessageInform(assistantMessageInform);
            ArrayList<AssistantListFormKey> assistantListFormKeys=assistantService.findAssistantByID(doctorId);
            for(int i=0;i<assistantListFormKeys.size();i++)
            {
                if(assistantListFormKeys.get(i).getAssistantId()==assistantId) {
                    continue;
                }
                assistantMessageInform=new AssistantMessageInform(assistantListFormKeys.get(i).getAssistantId(),messageId,"未读",0);
                assistantMessageListService.insertCancelAssistentMessageInform(assistantMessageInform);
            }
            System.out.println(b+" "+messageId);
            try{
                OrderForm orderForm2=assistantMessageListService.getIt(orderForm);
                System.out.println(orderForm2.getBootId());
            }
            catch (Exception e){
                e.printStackTrace();
                result="False";
            }
            int c=assistantMessageListService.deleteOrderForm(orderForm);
            System.out.println("messageId="+messageId+" "+"b="+b+" "+"c="+c);
        }
        catch (Exception e){
            result="False";
        }
        return  result;
    }
}
